package capimServer;

import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.swing.text.html.ListView;

class Port{
	int number;
	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}
	
	public Port(int number) {
		this.number = number;
	}
}

/**
 * Control the free that can be used to attend new conections from clients
 * @author danielm pessoafpc
 *
 */
public class PortControl {
	Port a,b,c,d,e;
	List<Port> freePorts;
	
	/**
	 * Default contructor
	 */
	public PortControl() {
		a=new Port(9101);
		b=new Port(9102);
		c=new Port(9103);
		d=new Port(9104);
		e=new Port(9105);
		this.freePorts = new LinkedList<Port>();
		freePorts.add(a);
		freePorts.add(b);
		freePorts.add(c);
		freePorts.add(d);
		freePorts.add(e);
	}
	
	/**
	 * Get a free port to use
	 * @return port number to use
	 * @throws Exception
	 */
	public synchronized int getPort() throws Exception{
		Port temp;
		if(this.freePorts.isEmpty()){
			throw new Exception ("Insuficient Ports");
		} else {
			temp = freePorts.remove(0);
			return(temp.getNumber());
		}
	}
	
	/**
	 * Release a port for use
	 * @param portNumber number of port free
	 */
	public synchronized void releasePort(int portNumber){
		Port releasedPort = new Port(portNumber);
		freePorts.add(releasedPort);
	}
}
